﻿
//***********************************************************************
// * Module:   MsjZaAqjsffcpscHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/29
// * Purpose: Definition of the Class MsjZaAqjsffcpscHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjZaAqjsffcpscHandler 的摘要说明
    /// </summary>
    public class MsjZaAqjsffcpscHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                MsjZaAqjsffcpsc mMsjZaAqjsffcpsc = new MsjZaAqjsffcpsc();
                mMsjZaAqjsffcpsc.Jyid = Guid.NewGuid().ToString(); //主键
                mMsjZaAqjsffcpsc.Dwmc = context.Request.Form["texDwmc"]; //单位名称
                mMsjZaAqjsffcpsc.Frdb = context.Request.Form["texFrdb"]; //法人代表
                mMsjZaAqjsffcpsc.Dz = context.Request.Form["texDz"]; //地址
                mMsjZaAqjsffcpsc.Jjxz = context.Request.Form["texJjxz"]; //经济性质
                mMsjZaAqjsffcpsc.Dh = context.Request.Form["texDh"]; //电话
                mMsjZaAqjsffcpsc.Cz = context.Request.Form["texCz"]; //传真
                mMsjZaAqjsffcpsc.Yzbm = context.Request.Form["texYzbm"]; //邮政编码
                mMsjZaAqjsffcpsc.Cpmc = context.Request.Form["texCpmc"]; //产品名称
                mMsjZaAqjsffcpsc.Xh = context.Request.Form["texXh"]; //型号
                mMsjZaAqjsffcpsc.Cpzxbzmc = context.Request.Form["texCpzxbzmc"]; //产品执行标准名称
                mMsjZaAqjsffcpsc.Bzbh = context.Request.Form["texBzbh"]; //标准编号
                mMsjZaAqjsffcpsc.Cpsl = Convert.ToDecimal(context.Request.Form["texCpsl"]); //产品数量
                mMsjZaAqjsffcpsc.Cfdd = context.Request.Form["texCfdd"]; //存放地点
                mMsjZaAqjsffcpsc.Qysbsj = (context.Request.Form["texQysbsj"]).ToString(); //企业申报时间
                mMsjZaAqjsffcpsc.Sdgajgqssj = (context.Request.Form["texSdgajgqssj"]).ToString(); //市地公安机关签收时间
                mMsjZaAqjsffcpsc.Sgatsqsj = (context.Request.Form["texSgatsqsj"]).ToString(); //省公安厅签收时间
                mMsjZaAqjsffcpsc.Sgatslsj = (context.Request.Form["texSgatslsj"]).ToString(); //省公安厅受理时间
                mMsjZaAqjsffcpsc.Cycbdw = context.Request.Form["texCycbdw"]; //抽样承办单位
                mMsjZaAqjsffcpsc.Wtjyjgmc = context.Request.Form["texWtjyjgmc"]; //委托检验机构名称
                mMsjZaAqjsffcpsc.Bz = context.Request.Form["texBz"]; //备注
                mMsjZaAqjsffcpsc.Cjsj = DateTime.Now.ToString();
                mMsjZaAqjsffcpsc.Cjr = mUser.UserId; //context.Request.Form["texCjr"]; //创建人(人员ID)
                mMsjZaAqjsffcpsc.Cjrdw = mUser.OrgnaId; //创建单位(单位ID)
                //mMsjZaAqjsffcpsc.Xgr = context.Request.Form["texXgr"]; //修改人(人员ID)
                //mMsjZaAqjsffcpsc.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                //mMsjZaAqjsffcpsc.Xgsj = context.Request.Form["texXgsj"]; //修改时间(2013-05-28 09:13:01)
                mMsjZaAqjsffcpsc.Enabled = "1"; //是否有效（1有效，0无效） 删除
                mMsjZaAqjsffcpsc.Movesign = "0";//context.Request.Form["texMovesign"]; //是否传输（1已，0未）

                mResultModel = MsjZaAqjsffcpscBLL.Insert(mMsjZaAqjsffcpsc, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjZaAqjsffcpsc mMsjZaAqjsffcpsc = new MsjZaAqjsffcpsc();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjZaAqjsffcpscBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjZaAqjsffcpsc mMsjZaAqjsffcpsc = new MsjZaAqjsffcpsc();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjZaAqjsffcpsc = MsjZaAqjsffcpscBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjZaAqjsffcpsc != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mMsjZaAqjsffcpsc.Dwmc = context.Request.Form["texDwmc"]; //单位名称
                    mMsjZaAqjsffcpsc.Frdb = context.Request.Form["texFrdb"]; //法人代表
                    mMsjZaAqjsffcpsc.Dz = context.Request.Form["texDz"]; //地址
                    mMsjZaAqjsffcpsc.Jjxz = context.Request.Form["texJjxz"]; //经济性质
                    mMsjZaAqjsffcpsc.Dh = context.Request.Form["texDh"]; //电话
                    mMsjZaAqjsffcpsc.Cz = context.Request.Form["texCz"]; //传真
                    mMsjZaAqjsffcpsc.Yzbm = context.Request.Form["texYzbm"]; //邮政编码
                    mMsjZaAqjsffcpsc.Cpmc = context.Request.Form["texCpmc"]; //产品名称
                    mMsjZaAqjsffcpsc.Xh = context.Request.Form["texXh"]; //型号
                    mMsjZaAqjsffcpsc.Cpzxbzmc = context.Request.Form["texCpzxbzmc"]; //产品执行标准名称
                    mMsjZaAqjsffcpsc.Bzbh = context.Request.Form["texBzbh"]; //标准编号
                    mMsjZaAqjsffcpsc.Cpsl = Convert.ToDecimal(context.Request.Form["texCpsl"]); //产品数量
                    mMsjZaAqjsffcpsc.Cfdd = context.Request.Form["texCfdd"]; //存放地点
                    mMsjZaAqjsffcpsc.Qysbsj = context.Request.Form["texQysbsj"]; //企业申报时间
                    mMsjZaAqjsffcpsc.Sdgajgqssj = context.Request.Form["texSdgajgqssj"]; //市地公安机关签收时间
                    mMsjZaAqjsffcpsc.Sgatsqsj = context.Request.Form["texSgatsqsj"]; //省公安厅签收时间
                    mMsjZaAqjsffcpsc.Sgatslsj = context.Request.Form["texSgatslsj"]; //省公安厅受理时间
                    mMsjZaAqjsffcpsc.Cycbdw = context.Request.Form["texCycbdw"]; //抽样承办单位
                    mMsjZaAqjsffcpsc.Wtjyjgmc = context.Request.Form["texWtjyjgmc"]; //委托检验机构名称
                    mMsjZaAqjsffcpsc.Bz = context.Request.Form["texBz"]; //备注
                    //mMsjZaAqjsffcpsc.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                    //mMsjZaAqjsffcpsc.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjZaAqjsffcpsc.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                    mMsjZaAqjsffcpsc.Xgr = mUser.UserId; //修改人(人员ID)
                    mMsjZaAqjsffcpsc.Xgrdw = mUser.OrgnaId; //修改人单位(单位ID)
                    mMsjZaAqjsffcpsc.Xgsj = DateTime.Now.ToString(); //修改时间(2013-05-28 09:13:01)
                    mMsjZaAqjsffcpsc.Enabled = "1"; //是否有效（1有效，0无效）
                    mMsjZaAqjsffcpsc.Movesign = "0"; //是否传输（1已，0未）

                    mResultModel = MsjZaAqjsffcpscBLL.Update(mMsjZaAqjsffcpsc, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjZaAqjsffcpsc mMsjZaAqjsffcpsc = new MsjZaAqjsffcpsc(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjZaAqjsffcpsc = MsjZaAqjsffcpscBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjZaAqjsffcpsc != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjZaAqjsffcpsc);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string DWName = context.Request.Form["texDWName"];
            string CPName = context.Request.Form["texCPName"];
            string DBiao = context.Request.Form["texDb"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjZaAqjsffcpsc = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjZaAqjsffcpscConst.Columns.Enabled,"1"), SqlLogicOper.AND_LOGIC);
            if (!string.IsNullOrEmpty(DWName))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjZaAqjsffcpscConst.Columns.Dwmc, '%' + DWName + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(CPName))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjZaAqjsffcpscConst.Columns.Cpmc, '%' + CPName + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(DBiao))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjZaAqjsffcpscConst.Columns.Frdb, '%' + DBiao + '%'), SqlLogicOper.AND_LOGIC);
            }
            dtMsjZaAqjsffcpsc = Q.GetRecordByPage("*", sqllist.ToSql(), "CJSJ", intPageSize, intPageIndex, MsjZaAqjsffcpscConst.TableName, MsjZaAqjsffcpscConst.FieldKey, "DESC").Tables[0];
            if (dtMsjZaAqjsffcpsc != null && dtMsjZaAqjsffcpsc.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjZaAqjsffcpsc.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjZaAqjsffcpsc.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjZaAqjsffcpsc;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


